import requests
import openpyxl

cookies = {
    'bid': 'BR5WeN5eRs4',
    'douban-fav-remind': '1',
    'll': '"118174"',
    'ap_v': '0,6.0',
    '__utma': '30149280.389437695.1733364009.1733364009.1760276585.2',
    '__utmb': '30149280.0.10.1760276585',
    '__utmc': '30149280',
    '__utmz': '30149280.1760276585.2.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)',
    '_vwo_uuid_v2': 'D55FA38FBEB8A701D4608B640D3B92975|83906a913c7ff52792005d28548683de',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9',
    'origin': 'https://movie.douban.com',
    'priority': 'u=1, i',
    'referer': 'https://movie.douban.com/explore',
    'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
    # 'cookie': 'bid=BR5WeN5eRs4; douban-fav-remind=1; ll="118174"; ap_v=0,6.0; __utma=30149280.389437695.1733364009.1733364009.1760276585.2; __utmb=30149280.0.10.1760276585; __utmc=30149280; __utmz=30149280.1760276585.2.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _vwo_uuid_v2=D55FA38FBEB8A701D4608B640D3B92975|83906a913c7ff52792005d28548683de',
}

response = requests.get(
    'https://m.douban.com/rexxar/api/v2/movie/recommend?refresh=0&start=0&count=120&selected_categories=%7B%22%E7%B1%BB%E5%9E%8B%22:%22%E5%96%9C%E5%89%A7%22%7D&uncollect=false&score_range=0,10&tags=%E5%96%9C%E5%89%A7',
    cookies=cookies,
    headers=headers,
)

# print(response.json().get('items'))
data = response.json().get('items')

movie_list = []

for item in data:
    if item.get('type') == 'movie':
        title = item.get('title')

        card_subtitle = item.get('card_subtitle')

        count = item.get('rating').get('count')

        value = item.get('rating').get('value')

        large_pic =item.get('pic').get('large')
        movie_list.append([title, card_subtitle,  value, count,large_pic])
# print("movie_list:", movie_list)
workbook = openpyxl.Workbook()

sheet = workbook.active
sheet.append(["名字","副标题","评分","评分人数","海报"])
for movie_item in movie_list:
    sheet.append(movie_item)
workbook.save('douban.xlsx')